package us.gaaoc.framework.webapp.backingbeans;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.view.GenericFiling;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.controllers.model.TableView;
import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.dao.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jfree.util.Log;

import us.gaaoc.framework.dao.CaseRecordHistoryDAO;
import us.gaaoc.framework.dao.DAOFactory;
import us.gaaoc.framework.logic.validation.DateValidationLogic;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.DocumentInstance;
import us.gaaoc.framework.model.Roles;
import us.gaaoc.framework.util.io.FileUtility;
import us.gaaoc.framework.util.validators.DateValidator;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.util.Messages;
public class ReportTablePageController extends BaseBackingBean{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8905870571068078452L;
	/**
	 * Properties
	 */
	private static final Logger LOG = Logger.getLogger(ReportTablePageController.class);
	
	
	
	private List<Filing> data; /** The data backing the table */

	private boolean renderCaseDetailLink;
	private boolean renderNewFilingButton;

	public ReportTablePageController()
		{		
		
		  Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();

		System.out.println("==============================================================");
		System.out.println("I am in the Constructor of the Controller for the ReportTablePageController Page");
		System.out.println("==============================================================");
		
		
		Person p = (Person) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("person");
		int  StatusCodeforRendering = Integer.parseInt((String)sessionMap.get("SearchStatusCode"));
		Integer myint = (Integer) sessionMap.get("formatTypeSelected");
         String roleofUser = null;
		List <Roles>personRoleList = (List) p.getRoles();
		for(Roles role:personRoleList)
		{
			 roleofUser =(String )role.getRole();
		}
		
		if(roleofUser.equalsIgnoreCase("agent")){
			this.setRenderNewFilingButton(true);
		}
		
		if(roleofUser.equalsIgnoreCase("agent")){
			switch(myint)
			{
			case 1:
				if(StatusCodeforRendering==7 || StatusCodeforRendering==15)
				this.setRenderCaseDetailLink(true);
				break;
			
		case 2:
			if(StatusCodeforRendering==7 || StatusCodeforRendering==15)
				this.setRenderCaseDetailLink(true);
			
			break;
		case 3:
			if(StatusCodeforRendering==13 || StatusCodeforRendering==12)
				this.setRenderCaseDetailLink(true);
			
			break;
		}
			
		}else if(roleofUser.equalsIgnoreCase("attorney")){
			switch(myint)
			{
			case 1:
				if(StatusCodeforRendering==10 || StatusCodeforRendering==8 || StatusCodeforRendering==11|| StatusCodeforRendering==24)
				this.setRenderCaseDetailLink(true);
				break;
			
		case 2:
			if(StatusCodeforRendering==10 || StatusCodeforRendering==8 || StatusCodeforRendering==11|| StatusCodeforRendering==24)
				this.setRenderCaseDetailLink(true);
			
			break;
		case 3:
			if(StatusCodeforRendering==24 || StatusCodeforRendering==10|| StatusCodeforRendering==9|| StatusCodeforRendering==8)
				this.setRenderCaseDetailLink(true);
			
			break;
		}
			
		}else if(roleofUser.equalsIgnoreCase("clerk")){
			
			switch(myint)
			{
			case 1:
				if(StatusCodeforRendering==2 || StatusCodeforRendering==22 || StatusCodeforRendering==1|| StatusCodeforRendering==20)
				this.setRenderCaseDetailLink(true);
				break;
			
		case 2:
			if(StatusCodeforRendering==2 || StatusCodeforRendering==22 || StatusCodeforRendering==1|| StatusCodeforRendering==20)
				this.setRenderCaseDetailLink(true);
			
			break;
		case 3:
			if(StatusCodeforRendering==2 || StatusCodeforRendering==20|| StatusCodeforRendering==1|| StatusCodeforRendering==17)
				this.setRenderCaseDetailLink(true);
			
			break;
		}
		}
		
		
	

		System.out.println("The Status Code I have Received is :" + (String)sessionMap.get("SearchStatusCode"));

	
	
		
		
		
	}


	public List<Filing> getData() {
		/**
		 * Return the List compile in another method and give it to data
		 */

		if(data == null)
		{	
			data = this.getAllFilings();					
		}
		
		

		
		return data;
	}


	public void setData(List<Filing> data) {
		this.data = data;
	}
	

	public boolean isRenderCaseDetailLink() {
		return renderCaseDetailLink;
	}


	public void setRenderCaseDetailLink(boolean renderCaseDetailLink) {
		this.renderCaseDetailLink = renderCaseDetailLink;
	}


	public boolean isRenderNewFilingButton() {
		return renderNewFilingButton;
	}


	public void setRenderNewFilingButton(boolean renderNewFilingButton) {
		this.renderNewFilingButton = renderNewFilingButton;
	}


	/**
	 * Navigates the user to the existing filing page.
	 */
	public String gotoExistingFilingPage() {
		return "existingFilingPage";
	}
	
	/**
	 * Navigates the user to the search page.  Also, saves the current page
	 * to the session map.
	 */
	@SuppressWarnings("unchecked")
	public String gotoSearchPage() {
		Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
	 	sessionMap.put("from", "CustomFilingsPage");
		return "gotoSearchPage";
	}
	

	
	/**
	 * Navigates the user to the new filings page.  Also, saves the current page
	 * to the session map.
	 */
	@SuppressWarnings("unchecked")
	public String createNewFiling() {
		Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		sessionMap.put("from", "CustomFilingsPage");
		sessionMap.remove("caseUuid"); // fix for issue GAJETWOX-124
		return "createNewFilingPage";
	}

	/**
	 * Navigates to the history of the case
	 * @requestparam caseUuid the UUID of the case record
	 */
	@SuppressWarnings("unchecked")
	public String gotoHistoryPage() {
		Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		Map requestMap = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "CustomFilingsPage");
		Person p = (Person) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("person");
        String roleofUser = null;
		List <Roles>personRoleList = (List) p.getRoles();
		for(Roles role:personRoleList)
		{
			 roleofUser =(String )role.getRole();
		}
		
		if(roleofUser.equalsIgnoreCase("agent"))
		{
		 	return "gotoCaseHistoryPage";

		}else if(roleofUser.equalsIgnoreCase("attorney"))
		{
		 	return "gotoCaseHistoryPage";

		}else if(roleofUser.equalsIgnoreCase("clerk"))
		{
		 	return "gotoCaseHistoryPage";

		}
		return null;
	}
	
	/* Navigation */
	
	/**
	 * Navigates the user to the case details page. 
	 * @requestparam caseUuid the UUID of the case.
	 */
	@SuppressWarnings("unchecked")
	public String gotoCaseDetailsPage() {
		Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		Map requestMap = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "CustomFilingsPage");
		
		Person p = (Person) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("person");
        String roleofUser = null;
		List <Roles>personRoleList = (List) p.getRoles();
		for(Roles role:personRoleList)
		{
			 roleofUser =(String )role.getRole();
		}
		
		if(roleofUser.equalsIgnoreCase("agent"))
		{
		 	return "gotoAgentCSECaseDetailsPage";

		}else if(roleofUser.equalsIgnoreCase("attorney"))
		{
		 	return "gotoAttorneyCSECaseDetailsPage";

		}else if(roleofUser.equalsIgnoreCase("clerk"))
		{
		 	return "gotoClerkCSECaseDetailsPage";

		}
		return null;
	 	
	}
	
	public List<Filing> getAllFilings()
	{
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
		 String CourtUuid = (String) sessionMap.get("courtUuid");

		Date InstantiationStartDate = (Date)sessionMap.get("CourtStartDate");
		System.out.println("The Instantiation Start Date I have Received is :" + InstantiationStartDate);

		Date InstantiationEndDate = (Date)sessionMap.get("CourtEndDate");
		System.out.println("The Instantiation End Date I have Received is :" + InstantiationEndDate);

		int  StatusCode = Integer.parseInt((String)sessionMap.get("SearchStatusCode"));
		System.out.println("The Status Code I have Received is :" + (String)sessionMap.get("SearchStatusCode"));

		Integer myint = (Integer) sessionMap.get("formatTypeSelected");
		
		
		if(myint==1)
		{
			List<Filing> data = this.getModelFacade().findAllCaseRecordByStatusCodeandLatestEventDateTime(StatusCode, CourtUuid, InstantiationStartDate, InstantiationEndDate);
			return data;

		}
		else if(myint==2)
		{
			List<Filing> data =this.getModelFacade().findAllCaseRecordByStatusandCreatedDate(StatusCode, CourtUuid,InstantiationStartDate, InstantiationEndDate);
			return data;

		}
		else if(myint==3)
		{
			List<Filing> data =this.getModelFacade().findAllCaseRecordByEventCodeandLatestEventDateTime(StatusCode, CourtUuid, InstantiationStartDate, InstantiationEndDate);
			return data;

		}
		else if(myint==4)
		{
			List<Filing> data =this.getModelFacade().findAllCaseRecordByEventCodeandEventDateTime(StatusCode, CourtUuid, InstantiationStartDate, InstantiationEndDate);
			return data;

		}
		
		return this.getModelFacade().findAllCaseRecordByStatusCodeandLatestEventDateTime(StatusCode, CourtUuid, InstantiationStartDate, InstantiationEndDate);

		
	}
 
	public String back()
	{

		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();;
	
		//sessionMap.remove("courtUuid");
		sessionMap.remove("CourtStartDate");
		sessionMap.remove("CourtEndDate");
		sessionMap.remove("SearchStatusCode");
		sessionMap.remove("formatTypeSelected");
		
		return "backToReports";
	
	}
	
}
